﻿using System;
using System.Data.Linq;
using System.Configuration;
using System.IO;

namespace HomeDMSObjectModel
{
    public class DBContextFactory
    {
        private static DataContext _dataBase;

        public static string CurrentDB { get; set; }

        public static DataContext GetCurrentContext()
        {
            if (_dataBase == null)
            {
                if (string.IsNullOrEmpty(CurrentDB))
                {
                    throw new NullReferenceException("Current data base is not assigned");
                }
                _dataBase = new HomeDMSContext(CurrentDB);

                string sqlLogFile = ConfigurationManager.AppSettings["LogSql"];
                if (!string.IsNullOrEmpty(sqlLogFile))
                {
                    StreamWriter sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + sqlLogFile);
                    _dataBase.Log = sw;
                }
            }

            return _dataBase;
        }

        public static void ClearContext()
        {
            _dataBase = null;
        }
    }
}
